package pers.qianyu.month_202102.date_20210224;

/**
 * 832. 翻转图像
 * https://leetcode-cn.com/problems/flipping-an-image/
 *
 * @author mizzle rain
 * @date 2021-02-24 11:29
 */
public class FlipAndInvertImage {
    public int[][] flipAndInvertImage(int[][] A) {
        int m = A.length, n = A[0].length;
        for (int i = 0; i < m; i++) {
            for (int left = 0, right = n - 1; left < right; right--, left++) {
                int temp = A[i][left];
                A[i][left] = A[i][right];
                A[i][right] = temp;
            }
            for (int j = 0; j < n; j++) A[i][j] ^= 1;
        }
        return A;
    }

    public int[][] flipAndInvertImage2(int[][] A) {
        int m = A.length, n = A[0].length;
        for (int i = 0; i < m; i++) {
            int left = 0, right = n - 1;
            for (; left < right; right--, left++) {
                if (A[i][left] == A[i][right]) {
                    A[i][left] ^= 1;
                    A[i][right] ^= 1;
                }
            }
            if (left == right) A[i][right] ^= 1;
        }
        return A;
    }
}
